$(document).ready(function(){
	var VAL = {
			T_FAPUASGRP : "FAPUASGRP",
			NORMAL : "0000000",
		    EVT_USR : $("#userId").text(),
			DISABLED_ATTR : {
				"disabled" : true
			},
			ENABLED_ATTR : {
				"disabled" : false
			}
		};
	var domObj = {
			W   : $(window),
			
			$usrListGrd : $("#usrListGrd"),
			$usrListDiv : $("#usrListDiv"),
			usrListPg : "usrListPg",
			
			$usrIdTxt   :  $("#usrIdTxt  "),
			$usrNameTxt :  $("#usrNameTxt"),
			$engNameTxt :  $("#engNameTxt"),
			//$usrCateSel :  $("#usrCateSel"),
		    $deptTxt:      $("#deptTxt"),
			$regDateTxt :  $("#regDateTxt"),
			$phoneTxt   :  $("#phoneTxt  "),
			$telTxt     :  $("#telTxt    "),
			$mailTxt    :  $("#mailTxt   "),
			$validFlg   :  $("#validFlg  "),
			$lockFlg    :  $("#lockFlg   "),
			
			buttons : {
				$query_btn 		: $("#query_btn"),
		        $delete_btn 	: $("#delete_btn"),
		        $update_btn     : $("#update_btn"),        
				$add_btn        : $("#add_btn"),
				$register_btn   : $("#register_btn")
			}
		};

	var controlsFunc = {
			iniUsrGrd : function(){
				var itemInfoCM = 
					   [{  name  : 'usr_id'    ,  index : 'usr_id',  label :  USER_ID_TAG  ,width : 180 },
						{  name  : 'usr_name'    ,  index : 'usr_name',   label : USER_NAME_TAG,width : 160 }];
				domObj.$usrListGrd.jqGrid({
					datatype : "local",
					autoheight : true,
					mtype : "POST",
					autowidth : true,//宽度根据父元素自适应
                    shrinkToFit:true,
                    autoScroll: false,
					scroll : true,
					resizable : true,
					rownumbers : true,
					loadonce : true,
					viewrecords : true,
					colModel : itemInfoCM,
					pager : domObj.usrListPg,
					onSelectRow : function(id) {
						controlsFunc.selRowFnc(id);
					}
				});
			},
			selRowFnc : function (id){
				/*if(domObj.$usrIdTxt.attr("disabled") != "disabled"){
					showErrorDialog("", "目前处于编辑中，请勿进行查询操作");
					return false;
				}*/
				var rowData, iary, inObj, outObj, oary;
				$("input").attr(VAL.DISABLED_ATTR);
				$("select").attr(VAL.DISABLED_ATTR);
				
				rowData = domObj.$usrListGrd.jqGrid("getRowData", id);
				usr_id = rowData.usr_id;

				inObj = {
					trx_id : VAL.T_FAPUASGRP,
					action_flg : "Q",
					iary : [{
						usr_id : usr_id
					}]
				};
				outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code == VAL.NORMAL) {
					oary = outObj.oary[0];
					if (!oary) {
						return false;
					}

					domObj.$usrIdTxt.val(oary.usr_id);
					domObj.$usrNameTxt.val(oary.usr_name);
					domObj.$engNameTxt.val(oary.usr_e_name);
					// domObj.$usrCateSel.val(oary.usr_typ);
					//SelectDom.setSelect(domObj.$usrCateSel,oary.usr_typ);
					if(oary.reg_timestamp){
                        $("#regDateTxt input").val(oary.reg_timestamp.split(" ",2)[0]);
					}
					domObj.$deptTxt.val(oary.dept_id);
					domObj.$phoneTxt.val(oary.usr_sms);
					domObj.$telTxt.val(oary.usr_phs);
					domObj.$mailTxt.val(oary.usr_mail);
					if (oary.valid_flg === "Y") {
						domObj.$validFlg.attr('checked','checked');
					}else {
                        domObj.$validFlg.attr('checked',false);
					}
					if (oary.lock_flg === "Y") {
						domObj.$lockFlg.attr('checked','checked');
					}else{
                        domObj.$lockFlg.attr('checked',false);
					}
				}
			},
			// iniUsrCateFnc : function() {
			// 	SelectDom.setSelect(domObj.$usrCateSel , "G");
			// }
	};
	var btnFnc = {
			queryFunc:function(){
				
				$("#usrSelectDialog").modal({
						backdrop:true,
						keyboard:false,
						show:false
				});
				$("#usrSelectDialog").unbind('shown.bs.modal');
				$("#usrSelectDialog_queryBtn").unbind('click');
				$("#usrSelectDialog").bind('shown.bs.modal');
				$("#usrSelectDialog").modal("show");
				
				var usr_id =$("#usrSelectDialog_idTxt").val();
				$("#usrSelectDialog_queryBtn").bind('click',dialogUsrFnc);
				$("#usrSelectDialog_idTxt").attr({'disabled':false});
		    	$("#usrSelectDialog_idTxt").val("");
			},
			deleteFunc : function(){
	    		var selectRowId = domObj.$usrListGrd.jqGrid("getGridParam","selrow");
	    	    if(!selectRowId){
	    	    	showErrorDialog("","请选择要删除的用户信息！");
	    	    	return false;
	    	    } 
	    	    var rowData = domObj.$usrListGrd.jqGrid("getRowData",selectRowId);
	    	    $("#f1_query_btn").showCallBackWarnningDialog({
	    	    	errMsg  : "是否删除代码为:["+ rowData.usr_id +"]的用户,请确认!!!!",
	    	    	callbackFn : function(data) {
	    	    		if(data.result==true){
	    	    			var iary = [{
	    	    					usr_id : rowData.usr_id
	    	    			}]
	    	    			var inTrxObj={
	    	    					trx_id     : VAL.T_FAPUASGRP,
	    	    					action_flg : "D"  ,
	    	    					iary       : iary
	    	    			}
	    	    			var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	    	    			if(outTrxObj.rtn_code === _NORMAL){
	    	    				showSuccessDialog("用户信息删除成功！！！");
	    	    				domObj.$usrListGrd.jqGrid("delRowData",selectRowId);
	        	                $("input").val("");
//	        	                $("select").empty();
	        	                SelectDom.setSelect($("select"), "", "");
	    	    			}
	    	    		}
	    	    	}
	    	    });
	    	},
	    	addFunc : function(){
	    		if(domObj.$usrIdTxt.attr("disabled") != "disabled"){
					showErrorDialog("", "已经在新增中，请勿重复新增");
					return false;
				}
	    		SelectDom.setSelect($("select"), "", "");
                $("input").attr(VAL.ENABLED_ATTR);
                $(":input").val("");
	    		// controlsFunc.iniUsrCateFnc();
//				$("select").attr(VAL.ENABLED_ATTR);
				$(":checkbox").removeAttr("checked");
                domObj.$usrIdTxt.focus();
            },
	    	registerFunc : function(){
	    		var actionFlg, inObj, outObj, iary, newRowId,validFlg='N',lockFlg='N';
	    		actionFlg = domObj.$usrIdTxt.attr("disabled") === "disabled"
					? "U"
					: "A";
	    		var usrIdTxt   = domObj.$usrIdTxt  .val();
	    		var usrNameTxt = domObj.$usrNameTxt.val();
	    		var engNameTxt = domObj.$engNameTxt.val();
	    		//var usrCateSel = domObj.$usrCateSel.val();
	    		var regDateTxt = $("#regDateTxt input").val();
	    		var deptTxt=   domObj.$deptTxt.val();
	    		var phoneTxt   = domObj.$phoneTxt  .val();
	    		var telTxt     = domObj.$telTxt    .val();
	    		var mailTxt    = domObj.$mailTxt   .val();
	    		if (domObj.$validFlg.attr("checked")) {
	    			validFlg   = 'Y';
				}
	    		if (domObj.$lockFlg.attr("checked")) {
	    			lockFlg   = 'Y';
				}
	    		if (!usrIdTxt) {
					showErrorDialog("", "用户工号不能为空");
					return false;
				}
	    		if (!usrNameTxt) {
					showErrorDialog("", "用户姓名不能为空");
					return false;
				}
/*	    		if (!engNameTxt) {
					showErrorDialog("", "英文名不能为空");
					return false;
				}
	    		if (!regDateTxt) {
					showErrorDialog("", "入职日期不能为空");
					return false;
				}
                // if (!usrCateSel) {
				// 	showErrorDialog("", "用户类型不能为空");
				// 	return false;
				// }
	    		if (!phoneTxt) {
					showErrorDialog("", "座机号不能为空");
					return false;
				}
	    		if (!telTxt) {
					showErrorDialog("", "手机号不能为空");
					return false;
				}
	    		if (!mailTxt) {
					showErrorDialog("", "邮件地址不能为空");
					return false;
				}*/
	    		iary = {
	    			usr_id		 :    usrIdTxt   ,
	    			usr_name	 :    usrNameTxt ,
	    			usr_e_name	 :    engNameTxt ,
	    			//usr_typ		 :    usrCateSel ,
	    			reg_timestamp:    regDateTxt ,
	    			dept_id      :    deptTxt,
	    			usr_phs      :    telTxt   ,
	    			usr_sms      :    phoneTxt     ,
	    			usr_mail     :    mailTxt    ,
	    			valid_flg 	 :    validFlg   ,
	    			lock_flg     :    lockFlg
	    		}
	    		inObj = {
	    				trx_id : VAL.T_FAPUASGRP,
	    				action_flg : actionFlg,
	    				evt_usr : VAL.EVT_USR,
	    				iary : [iary]
	    			};
	    		var outObj = comTrxSubSendPostJson(inObj);
	    		if (outObj.rtn_code == VAL.NORMAL) {
					
					$("input").attr(VAL.DISABLED_ATTR);
					$("select").attr(VAL.DISABLED_ATTR);
					
					if (actionFlg == "A") {
						var rowIds = domObj.$usrListGrd.jqGrid("getDataIDs");
						newRowId = rowIds.length > 0 ? (rowIds[rowIds.length - 1]) + 1 : "1";
						domObj.$usrListGrd.jqGrid("addRowData", newRowId,iary);
						domObj.$usrListGrd.setSelection(newRowId, true); 
						controlsFunc.selRowFnc(newRowId);
						showSuccessDialog("新增用户信息成功");
					} else if (actionFlg == "U") {
						var rowid = domObj.$usrListGrd.jqGrid('getGridParam','selrow');
						controlsFunc.selRowFnc(rowid);
						showSuccessDialog("用户信息更新成功");
					}
				}
	    		
	    	},
	    	updateFunc : function() {
				var selRowId = domObj.$usrListGrd.jqGrid("getGridParam","selrow");
				if (!selRowId) {
					showErrorDialog("", "请选择需要修改的用户信息");
					return false;
				}
				$("input:not(#usrIdTxt)").attr(VAL.ENABLED_ATTR);
//				$("#select").attr(VAL.ENABLED_ATTR);
			}
	      };
	   function dialogUsrFnc(usr_id){
		var iary = {};
		var usr_id =$("#usrSelectDialog_idTxt").val();
    	if(usr_id !=""){
    		iary.usr_id  = usr_id ;
    	}
    	
    	 var inTrxObj ={
    			 trx_id     : VAL.T_FAPUASGRP ,
    			 action_flg : "Q"        ,
    			 iary       : [iary]
    	 }
    	 
    	 var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
    	 if(  outTrxObj.rtn_code == _NORMAL ) {
    		 setGridInfo(outTrxObj.oary,"#usrListGrd");
    		 $('#usrSelectDialog').modal("hide");
    	 }
    }
	
	function iniClearFunc(){
		$("input").val("");
        $("select").empty();
		domObj.$usrListGrd.jqGrid("clearGridData");
		$("input").attr(VAL.DISABLED_ATTR);
		$("select").attr(VAL.DISABLED_ATTR);
	}
	
	function iniButtonAction(){
		domObj.buttons.$query_btn.click(function(){
			btnFnc.queryFunc();
		});
		domObj.buttons.$delete_btn.click(function(){
			btnFnc.deleteFunc();
		});
		domObj.buttons.$update_btn.click(function(){
			btnFnc.updateFunc();
		});
		domObj.buttons.$add_btn.click(function(){
			btnFnc.addFunc();
		});
		domObj.buttons.$register_btn.click(function(){
			btnFnc.registerFunc();
		});
		 $("#usrSelectDialog_idTxt").keydown(function(event){
	        	if (event.keyCode == 13 ) {
	        		var usr_id =$("#usrSelectDialog_idTxt").val();
	        		dialogUsrFnc(usr_id);
				}
	        });
	}
    function iniDatePicker(){
        var datepickerData;
        domObj.$regDateTxt.datetimepicker({
            language : 'zh-CN',
            autoclose : true,
            pickTime : false
        });
        domObj.$regDateTxt.datetimepicker().on(
            'changeDate',
            function(ev) {
                domObj.$regDateTxt
                    .datetimepicker('hide');
            });
//		datepickerData = domObj.$lrDatePicker.data("datetimepicker");
//		datepickerData.setLocalDate(datepickerData.getLocalDate());
    }
	 var otherActionBind = function(){
	        //Stop from auto commit
	        $("form").submit(function(){
	            return false;
	        });
	       
	    };
	function initFunc(){
		iniClearFunc();
        iniDatePicker();
		//controlsFunc.iniUsrCateFnc();
		controlsFunc.iniUsrGrd();
		iniButtonAction();
		otherActionBind();
	}
	initFunc();
	
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth,offsetBottom1, divWidth1;
        divWidth = $("#usrListDiv").width();
        offsetBottom =  $(window).height() - $("#usrListDiv").offset().top;
        $("#usrListDiv").height(offsetBottom * 0.95);
        $("#usrListGrd").setGridWidth(divWidth * 0.95);
        $("#usrListGrd").setGridHeight(offsetBottom * 0.99 - 101);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});